package com.zhongge.gui;

import com.zhongge.book.Book;
import com.zhongge.book.BookList;
import com.zhongge.operations.SearchBook;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 查找图书对话框
 */
public class SearchBookDialog extends JDialog {
    private BookList bookList;
    private JTextField nameField;
    private boolean searchCompleted = false;
    private JTextArea resultArea;
    
    public SearchBookDialog(Frame parent, BookList bookList) {
        super(parent, "查找图书", true);
        this.bookList = bookList;
        initUI();
        setSize(400, 300);
        setLocationRelativeTo(parent);
    }
    
    /**
     * 初始化界面
     */
    private void initUI() {
        JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
        
        // 输入面板
        JPanel inputPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JLabel nameLabel = new JLabel("书名: ");
        nameField = new JTextField(20);
        JButton searchButton = new JButton("查找");
        searchButton.addActionListener(new SearchListener());
        
        inputPanel.add(nameLabel);
        inputPanel.add(nameField);
        inputPanel.add(searchButton);
        
        // 结果显示面板
        JPanel resultPanel = new JPanel(new BorderLayout());
        JLabel resultLabel = new JLabel("查找结果:");
        resultArea = new JTextArea();
        resultArea.setEditable(false);
        resultArea.setLineWrap(true);
        resultArea.setWrapStyleWord(true);
        JScrollPane scrollPane = new JScrollPane(resultArea);
        
        resultPanel.add(resultLabel, BorderLayout.NORTH);
        resultPanel.add(scrollPane, BorderLayout.CENTER);
        
        // 按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JButton closeButton = new JButton("关闭");
        closeButton.addActionListener(e -> dispose());
        buttonPanel.add(closeButton);
        
        // 添加到主面板
        mainPanel.add(inputPanel, BorderLayout.NORTH);
        mainPanel.add(resultPanel, BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);
        
        setContentPane(mainPanel);
    }
    
    /**
     * 查找按钮监听器
     */
    private class SearchListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String bookName = nameField.getText().trim();
            if (bookName.isEmpty()) {
                JOptionPane.showMessageDialog(SearchBookDialog.this, 
                        "请输入书名", "提示", JOptionPane.WARNING_MESSAGE);
                return;
            }
            
            // 调用原有SearchBook实现查找功能
            SearchBook searchOperation = new SearchBook();
            
            // 创建自定义输出流捕获控制台输出
            ConsoleRedirector redirector = new ConsoleRedirector();
            redirector.startRedirect();
            
            try {
                // 由于SearchBook直接操作控制台，这里需要反射调用或重写
                // 为了不修改原有代码，这里采用模拟调用方式
                boolean found = false;
                StringBuilder result = new StringBuilder();
                
                for (int i = 0; i < bookList.getUsedSize(); i++) {
                    Book book = bookList.getBook(i);
                    if (bookName.equals(book.getName())) {
                        found = true;
                        result.append("查找成功：\n").append(book.toString());
                        break;
                    }
                }
                
                if (!found) {
                    result.append("查找失败：未找到名为《").append(bookName).append("》的图书");
                }
                
                resultArea.setText(result.toString());
                searchCompleted = true;
            } finally {
                redirector.stopRedirect();
            }
        }
    }
    
    /**
     * 判断查找是否完成
     */
    public boolean isSearchCompleted() {
        return searchCompleted;
    }
    
    /**
     * 控制台输出重定向工具类
     */
    private class ConsoleRedirector {
        private java.io.ByteArrayOutputStream outBuffer;
        private java.io.PrintStream originalOut;
        
        public void startRedirect() {
            originalOut = System.out;
            outBuffer = new java.io.ByteArrayOutputStream();
            System.setOut(new java.io.PrintStream(outBuffer));
        }
        
        public void stopRedirect() {
            System.setOut(originalOut);
            if (outBuffer != null) {
                resultArea.setText(outBuffer.toString());
            }
        }
    }
}